package com.kedou.app;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

/**
 * 众筹用户使用协议以及风险提示
 * 
 * @author zhouchao
 * 
 */
public class PromptActivity extends Activity implements OnClickListener {
	/**
	 * 左侧 返回按钮
	 */
	private Button mBtnBack;
	/**
	 * 中间 Title
	 */
	private TextView mTvTitle;
	private WebView mWebview;
	private ProgressBar progress;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_prompt);
		initView();
		setListener();
		Intent intent = getIntent();
		String prompt = intent.getStringExtra("prompt");
		if (prompt != null) {
			mTvTitle.setText(prompt);
		}
		loadWebview("http://www.jd.com/");
	}

	/**
	 * 初始化View
	 */
	private void initView() {
		mBtnBack = (Button) findViewById(R.id.btn_back);
		mBtnBack.setVisibility(View.VISIBLE);
		mTvTitle = (TextView) findViewById(R.id.title);
		mWebview = (WebView) findViewById(R.id.webview);
		progress = (ProgressBar) findViewById(R.id.progress);
		progress.setMax(100);
	}

	/**
	 * 设置监听器
	 */
	private void setListener() {
		mBtnBack.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_back:
			finish();
			break;
		}
	}

	/**
	 * 加载Webview
	 * 
	 * @param url
	 *            要加载的Url
	 */
	private void loadWebview(String url) {
		WebSettings settings = mWebview.getSettings();
		settings.setJavaScriptEnabled(true);
		mWebview.setInitialScale(100);// 初始显示比例100%
		settings.setSupportZoom(true);
		settings.setBuiltInZoomControls(true);
		settings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
		mWebview.setWebChromeClient(new WebViewClient());
		mWebview.loadUrl(url);
	}

	private class WebViewClient extends WebChromeClient {
		@Override
		public void onProgressChanged(WebView view, int newProgress) {
			progress.setProgress(newProgress);
			if (newProgress == 100) {
				progress.setVisibility(View.GONE);
			}
			super.onProgressChanged(view, newProgress);
		}
	}
}
